<?php

/**
 * 竞秀。
 */

define('IN_DISCUZ', true);
error_reporting(0);

class bid {
	var $bid_data = null;
	var $bid_index = null;

	static function &instance() {
		static $object;
		if(empty($object)) {
			$object = new bid();
			$object->init();
		}
		return $object;
	}

	function bid() {
		$this->bid_data = bid_data::instance();
		$this->bid_index = bid_index::instance();
	}

	function init(){

	}

	// 获得当前首页正在展示的宝宝照片。
	function get_baby_img(){
		return $this->bid_index->get_baby_img();
	}

	// 读取首页缩略图列表
	function get_baby_thumb_list(){
		return $this->bid_index->get_baby_thumb_list();
	}

	// 读取宝宝信息
	function get_baby_info(){
		return $this->bid_index->get_baby_info();
	}

	// 读取当前活动列表。默认读取当前正在竞秀的4个活动
	function get_vote_activity_list($status = 1){
		return 	$this->bid_data->get_vote_activity_list($status);
	}

	// 读取指定的活动。
	function get_vote_activity($aid){
		return $this->bid_data->get_vote_activity($aid);
	}

	// 从数据库中读取一个新的竞秀活动信息
	function add_vote_activity($activity){
		return $this->bid_data->add_vote_activity($activity);
	}

	// 读取指定活动的参赛选手信息
	function get_vote_user_list($aids){
		if (!is_array($aids)) {
			$aids = array($aids);
		}
		return $this->bid_data->get_vote_user_list($aids);
	}

	// 读取指定活动的参赛选手得分信息
	function get_vote_user_count_list($aids){
		if (!is_array($aids)) {
			$aids = array($aids);
		}
		return $this->bid_data->get_vote_user_count_list($aids);
	}

}



/**
 *
 * 当前首先正在显示的用户信息
 * @author limingju
 *
 */
class bid_index {
	var $default_baby_img = 'static/image/portal/index.jpg';
	var $load_sucess = false;
	// 当面首页正在显示的宝宝的UID
	var $baby_uid = 0;
	// 当面首页正在显示的宝宝的PICID
	var $baby_picid = 0;
	var $current = 0;
	var $current_week = 0;
	var $vote_activity;

	var $vote_activity_list =array();
	var $vote_activity_id_map = array();
	var $vote_user_list = array();
	var $vote_user_id_map = array();

	var $user = array();
	var $pic_list = array();
	var $pic_map = array();
	var $thumb_list = array();
	var $bid_data = null;

	static function &instance() {
		static $object;
		if (empty($object)){
			$object = new bid_index();
			$object->init();
		}
		return $object;
	}

	function bid_index() {
		$this->bid_data = bid_data::instance();
		$this->current = date('U');
		$this->current_week = date('W');
	}

	function init() {
		$this->vote_activity = $this->bid_data->get_img_show_activity($this->current);
		if ( empty($this->vote_activity) || empty($this->vote_activity['first_uid']) || empty($this->vote_activity['id'])){
			$this->load_sucess = false;
			return;
		}

		$this->vote_user = $this->bid_data->get_vote_user($this->vote_activity['id'], $this->vote_activity['first_uid']);
		$this->merge_babyinfo($this->bid_data->get_profile_baby($this->vote_activity['first_uid']));
		$this->merge_address($this->bid_data->get_profile($this->vote_activity['first_uid']));

		// 读取宝宝详细信息
		if (is_array($this->vote_user) && $this->vote_user['uid'] > 0 && $this->vote_user['picid'] > 0) {
			$this->baby_uid = $this->vote_user['uid'];
			$this->baby_picid = $this->vote_user['picid'];

			$this->pic_list = $this->bid_data->get_home_pic($this->baby_uid);

			for ($i = 0, $n = count($this->pic_list); $i < $n; $i++) {
				$this->pic_list[$i]['filepath'] = ('data/attachment/album/'.$this->pic_list[$i]['filepath']);
				$this->pic_list[$i]['thumbpath'] = ($this->pic_list[$i]['filepath'].'.thumb.jpg');
			}
			for($i = 0; $i < 6; $i++ ){
				$this->thumb_list[] = $this->pic_list[$i];
			}
			$this->pic_maps = $this->bid_data->get_column_map($this->pic_list, 'picid');

			// 说明加载成功
			$this->load_sucess = true;
		}
		else {
			$this->load_sucess = false;
		}
	}

	/**
	 * 获得当前首页正在展示的宝宝照片。
	 */
	function get_baby_img(){
		if (!empty($this->baby_picid) && !empty($this->pic_maps[$this->baby_picid]) && !empty($this->pic_maps[$this->baby_picid]['filepath'])) {
			return $this->pic_maps[$this->baby_picid]['filepath'];
		}
		return $this->default_baby_img;
	}

	/**
	 * 获得当前首页正在展示的宝宝照片列表。
	 */
	function get_baby_thumb_list(){
		return $this->thumb_list;
	}

	/**
	 * 获得当前首页正在展示的宝宝相关信息。
	 */
	function get_baby_info(){
		return $this->vote_user;
	}

	function merge_babyinfo($babyinfo){
		$this->vote_user['name'] = $babyinfo['name'];
		$this->vote_user['nick_name'] = $babyinfo['nick_name'];
		$this->vote_user['father_name'] = $babyinfo['father_name'];
		$this->vote_user['mother_name'] = $babyinfo['mother_name'];
		$this->vote_user['age'] = $this->calc_age($babyinfo['birthyear'], $babyinfo['birthmonth'], $babyinfo['birthday']);
	}

	// 生成家庭地址的字符串
	function merge_address($address){
		if (trim($address['resideprovince']) != '') {
			$this->vote_user['address'] .= ($address['resideprovince'].' ');
		}

		if (trim($address['residecity']) != '') {
			$this->vote_user['address'] .= ($address['residecity'].' ');
		}

		if (trim($address['residedist']) != '') {
			$this->vote_user['address'] .= ($address['residedist'].' ');
		}

		if (trim($address['residecommunity']) != '') {
			$this->vote_user['address'] .= ($address['residecommunity'].' ');
		}

		if (trim($address['residesuite']) != '') {
			$this->vote_user['address'] .= ($address['residesuite'].' ');
		}
	}

	function calc_age($by, $bm, $bd){
		$cm=date('n');
		$cd=date('j');
		$age=date('Y') - $by - 1;
		if ($cm > $bm || $cm == $bm && $cd > $bd) {
			$age++;
			$month = $cm - $bm;
			if ($month == 0) {
				$month = 1;
			}
		}

		$result = ($age.'岁'.$month.'个月');
		if ($age == 0) {
			$result = ($month.'个月');
		}
		return $result;
	}


}


/**
 *
 * @author limingju
 *
 */
class bid_data {
	var $pagesize = 4;

	// 读取当前竞秀活动列表
	var $vote_activity_list = array();
	// 根据活动的标识读取活动对象
	var $vote_activity_map = array();
	// 根据活动的标识读取活动
	var $vote_activity_count_map = array();
	// 根据活动状态读取活动列表
	var $vote_activity_status_map = array(0 => array(), 1 => array(), 2 => array());

	//根据指定的aid读取属于该aid的所有vote_user
	var $vote_user_aid_map = array();
	// 根据aid,uid读取指定的vote_user
	var $vote_user_aid_uid_map = array();

	//根据指定的aid读取属于该aid的所有vote_user_count
	var $vote_user_count_aid_map = array();
	// 根据aid读取属于该活动的vote_user_count_list
	var $vote_user_count_aid_uid_map = array();


	static function &instance() {
		static $object;
		if(empty($object)) {
			$object = new bid_data();
		}
		return $object;
	}

	function bid_data(){

	}

	// 初始化加载投票活动相关信息。
	function init(){
		$this->load_vote_activity_list_db();
		$this->load_vote_activity_count_list_db();
		$this->load_vote_user_list_db();
		$this->load_vote_user_count_list_db();
	}


	//新增一个投票活动
	function add_vote_activity($activity){

	}

	// 读取指定的活动
	function get_vote_activity($aid){
		$activity = $this->vote_activity_map[$aid];
		if (!empty($activity)){
			return $activity;
		}
		return $this->get_vote_activity_db($aid);
	}

	// 读取当前周正在展示的宝宝照片
	function get_img_show_activity($img_time = 0){
		if ($img_time == 0){
			$img_time = date('U');
		}
		$sql = ' select * from '.DB::table('vote_activity').' where img_start_day <= '.$img_time.' and img_end_day > '.$img_time;
		$activity = DB::fetch_first($sql);
		// 缓存活动信息
		// cache_vote_activity($activity);
		return $activity;
	}

	// 读取宝宝信息。首页展示用。 TODO 如数据量大，需要缓存
	function get_profile_baby($uid){
		return DB::fetch_first(' select * from '.DB::table('common_member_profile_baby').' where uid = '.intval($uid));
	}

	// 读取用户信息。首页展示用。 TODO 如数据量大，需要缓存
	function get_profile($uid){
		return DB::fetch_first(' select * from '.DB::table('common_member_profile').' where uid = '.intval($uid));
	}

	// 读取指定用户空间照片信息
	function get_home_pic($uid){
		$sql = ' select * from '.DB::table('home_pic').' where uid = '.$uid.' order by dateline desc';
		return $this->query($sql);
	}

	// 加载参加活动的选手信息 。包括vote_user, vote_user_count
	function get_vote_user_list($aids){
		$vote_user_map = array();
		$new_aids = array();
		foreach ($aid as $aids) {
			if (empty($this->vote_user_aid_map[$aid])){
				$new_aids[] = $aid;
			}
		}
		$this->load_vote_user_list_db($new_aids);
		foreach ($aid as $aids) {
			if (!empty($this->vote_user_aid_map[$aid])){
				$vote_user_map[$aid] = $this->vote_user_aid_map[$aid];
			}
			else {
				$vote_user_map[$aid] = array();
			}
		}
		return $vote_user_map;
	}

	// 加载参加活动的选手得分信息。直接从试图中读取。
	function get_vote_user_count_list($aids){
		$vote_user_count_map = array();
		$new_aids = array();
		foreach ($aid as $aids) {
			if (empty($this->vote_user_count_aid_map[$aid])){
				$new_aids[] = $aid;
			}
		}
		$this->load_vote_user_count_list_db($new_aids);
		foreach ($aid as $aids) {
			if (!empty($this->vote_user_count_aid_map[$aid])){
				$vote_user_count_map[$aid] = $this->vote_user_count_aid_map[$aid];
			}
			else {
				$vote_user_count_map[$aid] = array();
			}
		}
		return $vote_user_count_map;
	}

	// 读取指定的SQL的结果集
	function query($sql){
		$rows = array();
		$query = DB::query($sql);
		while($row = DB::fetch($query)) {
			$rows[] =  $row;
		}
		return $rows;
	}

	// 读取指定的参赛选手
	function get_vote_user($aid, $uid){
		$vote_user =  $this->vote_user_aid_uid_map[$aid][$uid];
		if (!empty($vote_user)) {
			return $vote_user;
		}
		$vote_user = $this->get_vote_user_db($aid, $uid);
		$this->cache_vote_user($vote_user);
		return $vote_user;
	}

	// 从数据库中读取指定状态的活动信息。一般来说用来读取所有的活动信息
	function load_vote_activity_list_db($status = 1){
		$current = date('U');
		$sql = ' select * from '.DB::table('vote_activity').' where bid_start_day <= '.$current.' and bid_end_day > '.$current;
		if ($status != -1 ) {
			$sql .= (' and status = '.$status);
		}
		$query = DB::query($sql.' order by bid_end_day desc limit 4');
		while($vote_activity = DB::fetch($query)) {
			$this->cache_vote_activity($vote_activity);
		}
	}

	// 加载活动相关信息。并且缓存在hash表中
	function load_vote_activity_count_list_db($aids = array()){
		$sql = ' select * from '.DB::table('vote_activity_count');
		if (!empty($aids)) {
			$sql .= (' where aid in ('.implode($aids).') ');
		}
		array_merge($this->vote_activity_count_map, $this->get_column_map($this->query($sql), 'aid'));
	}

	// 加载参赛选手的信息
	function load_vote_user_list_db($aids = array()){
		$sql = ' select * from '.DB::table('vote_user');
		if (!empty($aids)) {
			$sql .= (' where aid in ('.implode($aids).') ');
		}
		$query = DB::query($sql);
		while($vote_user = DB::fetch($query)) {
			$this->cache_vote_user($vote_user);
		}
	}

	// 加载参赛选手得分的信息
	function load_vote_user_count_list_db($aids = array()){
		$sql = ' select * from '.DB::table('vote_user_count');
		if (!empty($aids)) {
			$sql .= (' where aid in ('.implode($aids).') ');
		}

		$aid = 0;
		$rank = 1;
		$query = DB::query($sql);
		while($vote_user_count = DB::fetch($query)) {
			if ($aid != $vote_user_count['aid']) {
				$rank = 1;
				$aid = $vote_user_count['aid'];
			}
			$this->cache_vote_user_count($vote_user_count, $rank++);
		}
	}

	// 从数据库中读取指定的活动。并且把指定的活动缓存起来
	function get_vote_activity_db($aid){
		$sql = ' select * from '.DB::table('vote_activity').' where id = '.$aid;
		$activity = DB::fetch_first($sql);
		$this->cache_vote_activity($activity);
		return $activity;
	}

	// 从数据库中读取参赛选手信息
	function get_vote_user_db($aid, $uid){
		$sql = ' select * from '.DB::table('vote_user').' where aid = '.intval($aid).' and uid = '.intval($uid);
		$vote_user = DB::fetch_first($sql);
		$this->cache_vote_user($vote_user);
		return $vote_user;
	}

	// 读取当前活动列表 TODO 此处需要把逻辑跟 get_vote_activity_list_db 联系起来
	function get_vote_activity_list($status = 1){
		// 读取所有的活动列表
		if ($status == -1 ) {
			if (empty($this->vote_activity_list)){
				$this->load_vote_activity_list_db($status);
			}

			return  $this->vote_activity_list;
		}
		if (empty($this->vote_activity_status_map[$status])){
			$this->load_vote_activity_list_db($status);
		}
		return  $this->vote_activity_status_map[$status];
	}

	// 把参赛选手缓存到数据库中
	function cache_vote_user(&$vote_user){
		if (empty($this->vote_user_aid_uid_map[$vote_user['aid']])) {
			$this->vote_user_aid_uid_map[$vote_user['aid']] = array();
		}
		$this->vote_user_aid_uid_map[$vote_user['aid']][$vote_user['uid']] = $vote_user;
	}

	// 参赛选手的得分信息
	function cache_vote_user_count(&$vote_user_count, $rank = 1){
		$vote_user_count['rank'] = $rank;
		$aid = $vote_user_count['aid'];
		$uid = $vote_user_count['uid'];

		$this->vote_user_count_aid_map[$aid][] = $vote_user_count;
		$this->vote_user_count_aid_uid_map[$aid][$uid] = $vote_user_count;
	}

	// 把指定的活动缓存到内存中
	function cache_vote_activity(&$activity){
		$this->format_activity($activity);
		$this->vote_activity_list[] = $activity;
		$this->vote_activity_map[$activity['id']] = $activity;
		$this->vote_activity_status_map[$activity['status']][] = $activity;
	}

	// 缓存之前对活动信息进行缓存
	function format_activity(&$activity){
		$activity['img_start_day_string'] = date('Y年m月d日', $activity['img_start_day']);
		$activity['img_end_day_string'] = date('Y年m月d日', $activity['img_end_day']);
		$activity['bid_start_day_string'] = date('Y年m月d日', $activity['bid_start_day']);
		$activity['bid_end_day_string'] = date('Y年m月d日', $activity['bid_end_day']);
		$activity['cache_time'] = time();
		if (0 == $activity['status']){
			$activity['status_string'] = '可编辑';
		} else if (1 == $activity['status']){
			$activity['status_string'] = '报名中';
		} else if (2 == $activity['status']){
			$activity['status_string'] = '竞拍中';
		} else if (3 == $activity['status']){
			$activity['status_string'] = '已关闭';
		}
	}

	function get_column_map($rows, $colname){
		$maps = array();
		foreach ($rows as $row) {
			$maps[$row[$colname]] = $row;
		}
		return $maps;
	}

	function get_column_list($rows, $colname){
		$cols = array();
		foreach ($rows as $row) {
			$cols[] = $row[$colname];
		}
		return $cols;
	}
}

?>